home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-09-01 | 8.6 KB | 249 lines | [TEXT/ALFA] |
- 0 28 +md !
- \
- \
- \ Quote.4th -- A very simple CGI application in Pocket Forth
- \
- \
- \ RTK, 05-23-95 Last mod: 09-01-95
- \
- \
-
- fvariable c 0.0 c ! \ holds quote count
- variable n 0 n ! \ holds number of quotes in database
-
- 0 fix \ set display mode
-
- \ Utility words
-
- : wsize ( h v -- ) 2dup 8 +md 2! 0 +md 2@ 2>r 2>r 256 >r ,$ A91D ;
-
- : random ( -- n' ) ( puts a random number from 1 to n on stack )
- 0 >r ,$ A861 r> abs n @ 32767 */ 1+ ;
-
- : ForeColor ( color -- ) 0 2>r ,$ A862 ;
- : BackColor ( color -- ) 0 2>r ,$ A863 ;
-
- : !FONT ( n -- ) >r ,$ A887 ; macro ( _TextFont ) ( set font )
- : !FSIZE ( n -- ) >r ,$ A88A ; macro ( _TextSize ) ( set size )
-
- : <> = 0= ;
-
- \ ============== from Datafiles example in PF 6.3 ====================
-
- : 00>R ( rstack: -- 0 0 ) ,$ 42A7 ; macro ( clr.l -[rs] )
-
- variable FCB 78 allot ( our File's Control Block )
- : +FCB ( offset -- addr ) fcb + ; ( offset into fcb )
- : 0FCB ( -- ) fcb 80 0 fill ;
- : FTRAP ( -- ) fcb >abs ,$ 205E ; ( movea.l [ps]+,a0 )
-
- : CLOSE ( -- ) ftrap ,$ A001 ftrap ,$ A013 ; ( close & flush )
- : ?DERROR ( -- ) 16 +fcb @ ?dup IF ( if result not zero )
- ." DiskError" . close abort THEN ; ( report & abort )
-
- : EOF ( -- dbytes ) ftrap ,$ A011 30 +fcb @ ; ( _GetEOF )
- : !SIZE ( bytes -- ) 38 +fcb ! ; ( set bytes-to-read or write )
- : !FILE ( -- ) ( set data in fcb to open file from sfreply )
- 0fcb pad 6 + @ 22 +fcb ! ( set vrefnum )
- pad 10 + >abs 18 +fcb 2! ( set name )
- 01 27 +fcb c! ; ( read only )
-
- 2variable $TEXT ,s TEXT $text 2!
- : OPEN ( -- ) ( select and open a file )
- 55 75 2>r ( top left corner )
- 00>r 00>r 1 >r $text a>r 00>r pad a>r ( reply at here )
- 2 >r ,$ A9EA ( _SFGetFile )
- pad @ IF ( check good field )
- !file ftrap ,$ A000 ?derror ( _Open the file )
- ELSE beep quit THEN ;
-
- : re-open ( -- ) ( open a file already selected )
- ftrap ,$ A000 ?derror ; ( _Open )
-
- : READ ( dabs.addr -- ) ( allows read outside of dictionary )
- 32 +fcb 2! ( set read buffer pointer )
- ftrap ,$ A002 ?derror ; ( _Read )
-
- : LIST ( -- )
- open eof dup 0< IF abs THEN ( determine file length )
- room 44 - min dup !size ( set bytes to be read )
- pad dup >abs read close swap type ; ( read & type data )
-
-
- \ ===================== from AppleEvents file in PF 6.3 =====================
-
- 2variable DDATA 4 allot
-
- \ Message is a defining word for setting up strings for REPLYing
- : MESSAGE[ \ compiling: ( -- ) enclose subsequent ']'ed string
- CREATE 93 word here c@ 1+ dup 2 mod + allot
- DOES> count ; \ runtime action: ( -- addr count )
-
- MESSAGE[ SERROR Empty stack!]
-
- ( get AEDesc handle from an Apple Event )
- : ?DESC ( d.key d.type -- desc.handle desc.type -1 or 0 )
- 0 >r ( room for error )
- 202 +md 2@ 2>r ( the AppleEvent handle )
- 2swap 2>r 2>r ( keyword and type )
- here a>r ( receiving address )
- ,$ 303C ,$ 812 ,$ A816 ( AEGetParamDesc: move #$812,d0 _Pack8 )
- r> 0= IF ( if there is no error )
- here 4 + 2@ here 2@ -1 ( get data & leave true )
- ELSE 0 THEN ; ( or else leave false )
-
- : -DESC ( addr.where.desc.is.stored -- error ) ( remove desc rec. )
- 0 >r a>r ( push room and descriptor )
- ,$ 303C ,$ 0204 ,$ A816 ( AEDisposeDesc: move #$0204,d0 _Pack8 )
- r> ;
-
- \ Reply to an Apple Event with a string
- : REPLY ( addr count -- ) \ **** USE INSIDE OF A HANDLER ONLY ****
- 0 >r \ put room for error on rstack
- 198 +md 2@ 2>r \ put the ReplyEvent handle on rstack
- ,s ---- 2>r ,s TEXT 2>r \ put keyword and type on rstack
- swap a>r 0 2>r \ put addr & count on rs from pstack
- ,$ 303C ,$ 0A0F ,$ A816 \ AEPutParamPtr: move #$A0F,d0 _Pack8
- r> drop ; \ ignore any error
-
- \ ==========================================================================
-
- create tString 2048 allot ( holds the output string )
- variable tCount 0 tCount ! ( length of the output string )
-
- : >number ( -- n ) \ convert pad into a number, assumes leading zeros
- 1000 pad c@ 48 - *
- 100 pad 1+ c@ 48 - * +
- 10 pad 2+ c@ 48 - * +
- pad 3 + c@ 48 - + ;
-
- : #quotes ( -- n ) \ read the first line and get number of quotes
- 5 !SIZE pad >abs read >number
- close re-open ;
-
- message[ s1 <html>]
- message[ s2 </html>]
-
- : >tString ( c -- ) \ put a character on the end of tString
- tString tCount @ + c! tCount @ 1+ tCount ! ;
-
- : strcpy ( addr len -- ) \ copy characters into tString
- >r dup r> + swap do
- r c@ >tString
- loop ;
-
- : startString ( -- ) ( load the header text into tString )
- 0 tCount ! s1 strcpy ; \ copy into tString
- : endString ( -- ) s2 strcpy ; ( load the ending text into tString )
-
- : skiplines ( n -- ) \ skips to beginning of n-th quote
- 2+ begin dup 0> while
- 1 !size pad >abs read pad c@
- begin 13 <> while
- pad >abs read pad c@
- repeat
- 1-
- repeat ;
-
- : getline ( -- ) \ get the current line into tString
- 1 !size pad >abs read pad c@
- begin dup 13 <> while
- >tString
- pad >abs read pad c@
- repeat drop ;
-
- : reset ( -- ) close re-open ; \ reset the file
-
- : gHead ( -- ) \ read the prefix string
- reset -1 skiplines getline reset ;
-
- : gBack ( -- ) \ read the "back" string
- reset 0 skiplines getline ;
-
- : getQuote ( -- ) \ pick a random number and output that quote
- reset \ reset the file
- #quotes n ! \ get the number of quotes in the file
- reset \ reset again
- startString \ stuff header info in string
- gHead \ read prefix string
- random skiplines \ skip to the right quote
- getline \ read a line into the string
- gBack \ read back string
- endString \ tack on end of string
- reset \ reset the file
- ;
-
- 2variable DSIZE \ this double variable holds the size of a string in dbuff
- variable DBUFF 2046 allot \ this block is filled with a text string
-
- ( get AE data from an Apple Event )
- : ?DATA ( d.key -- addr length -1 or 0 )
- 0 >r \ make room on stack for error
- 202 +md 2@ 2>r \ push theAppleEvent address
- 2>r ,s TEXT 2>r \ push keyword (from pstack) and desired type (TEXT)
- here a>r \ push an address to hold the actual type
- dbuff a>r \ push the data receiving address
- 2048 s>d 2>r \ max number of bytes to read
- dsize a>r \ push a variable to hold the actual size
- ,$ 303C ,$ 0E11 ,$ A816 \ AEGetParamPtr: move #$812,d0 _Pack8
- r> 0= IF \ if there is no error
- dbuff dsize 2@ drop -1 \ put address, count and true on pstack
- ELSE 0 THEN ; \ else false
-
- \ Startup screen
-
- : update ( -- ) \ update screen showing accesses
- 33 BackColor 30 ForeColor page ( setup colors )
- @pen 3 + !pen ( move down a bit on the screen )
- 0 !FONT 12 !FSIZE ( Chicago )
- ." Quoter, ver 1.1, RTK, 09/95 "
- 1 !FONT 9 !FSIZE ( Geneva )
- ." This program has been accessed " c f@
- fdup 1.0 fcompare 0= >r fdrop fdrop r> IF
- f. ." time."
- ELSE f. ." times."
- THEN
- 4 !FONT 9 !FSIZE ( Monaco ) ( output IP address )
- cr cr ." Last access from: "
- dbuff dsize 2@ drop type
- 30000 10 !pen ( move pen off screen )
- ;
-
- : startup ( -- ) \ startup word
- 512 40 wsize ( set window size )
- 32 dbuff c! ( ' ' as first buff char )
- 1 0 dsize 2! ( set IP address length )
- OPEN ( get quotation database )
- update ( draw screen )
- begin key drop again ; ( listen to events )
-
-
- \ Install the 'sdoc' handler.
-
- ,s sdoc ,s WWWΩ ae:
- ,s addr ?data IF
- drop drop ( ?data puts the string in dbuff )
- THEN
- c f@ 1.0 f+ c f! Update ( increment counter )
- ,s ---- ,s TEXT ?desc IF
- ddata 2! ddata 4 + 2!
- ddata -desc 0= IF
- getQuote ( lookup the quote )
- tString ( address of beginning of the response )
- tCount @ ( length of the response )
- ELSE ." Oops!" cr THEN
- ELSE ." Nothing" cr THEN
- reply
- close re-open ( reset the database file )
- ;ae
-
-
- \ setup initialization pointers
-
- ' startup 26 +md ! ( startup word )
-
- ' update 12 +md ! ( activate window )
- ' update 14 +md ! ( update window )
-
- ' bye 18 +md @ @ ! ( first File menu option )
-